package com.riki.riki_push;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.meizu.cloud.pushsdk.PushManager;
import com.riki.riki_push.util.CommonUtil;
import com.riki.riki_push.util.MzSingleton;
import com.vivo.push.IPushActionListener;
import com.vivo.push.PushClient;
import com.xiaomi.mipush.sdk.MiPushClient;

import java.lang.reflect.Method;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * RikiPushPlugin
 */
public class RikiPushMeizuPlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    public static MethodChannel channel;
    private Context context;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "riki_push_meizu");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {


        try {
            if (call.method.equals("initMeizu")) {
                initMeizu(call, result);
            } else if (call.method.equals("getMeizuRegId")) {
                getMeizuRegId(call, result);
            } else if (call.method.equals("unInit")) {
                unInit(call, result);
            } else {
                result.notImplemented();
            }
        } catch (Exception e) {
            result.notImplemented();
        }


        /*try {
            Method method = this.getClass().getDeclaredMethod(call.method, MethodCall.class, Result.class);
            method.invoke(this, call, result);
        } catch (Exception e) {
            result.notImplemented();
        }*/
    }


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    /**
     * 注册 oppo
     */
    private void initMeizu(@NonNull MethodCall call, @NonNull Result result) {
        // 初始化Push服务
        try {
            String appId = CommonUtil.getParam(call, result, "appId");
            String appKey = CommonUtil.getParam(call, result, "appKey");
            PushManager.register(context, appId, appKey);
            result.success(1);
        } catch (Exception e) {
            result.success(0);
        }
    }


    /**
     * 获取魅族 ID
     */
    private void getMeizuRegId(@NonNull MethodCall call, @NonNull Result result) {
        try {
            String singleton = MzSingleton.getSingleton().getMzPushId();
            result.success(singleton);
        } catch (Exception e) {
            result.success(0);
        }
    }


    /**
     * 反初始化
     */
    private void unInit(@NonNull MethodCall call, @NonNull Result result) {
        try {
            String appId = CommonUtil.getParam(call, result, "appId");
            String appKey = CommonUtil.getParam(call, result, "appKey");
            PushManager.unRegister(context, appId, appKey);
            result.success(1);
        } catch (Exception e) {
            result.success(0);
        }
    }


}
